<script setup>
import { ref } from 'vue'
import { aiMessageService } from '@/api/aichat'
import { marked } from 'marked'
import { useUserStore } from '@/stores'
import { useMessageStore } from '@/stores'

const userStore = useUserStore()
const messageStore = useMessageStore()
const userInput = ref('')
const messages = ref(messageStore.messages)
const isLoading = ref(false)

const sendMessage = async () => {
  if (!userInput.value.trim() || isLoading.value) return

  messages.value.push({ from: 'user', text: userInput.value })
  const userMessage = userInput.value
  userInput.value = ''
  isLoading.value = true
  const res = await aiMessageService({ userMessage: userMessage, id: userStore.user.id })
  if (res.data.code == 1) {
    const aiReplyMarkdown = res.data.data
    const aiReplyHtml = marked.parse(aiReplyMarkdown) // 解析 Markdown 为 HTML
    messages.value.push({ from: 'ai', text: aiReplyHtml })
  } else {
    ElMessage.error(res.data.msg)
    messages.value.push({ from: 'ai', text: res.data.msg })
  }
  isLoading.value = false
}
</script>
<template>
  <div class="chat-window">
    <div class="chat-header">AI Chat</div>
    <div class="chat-body">
      <div
        v-for="(message, index) in messages"
        :key="index"
        :class="{ 'user-message': message.from === 'user', 'ai-message': message.from === 'ai' }"
      >
        <div class="avatar">
          {{ message.from === 'user' ? '👤' : '🤖' }}
        </div>
        <div class="message-content">
          <div
            class="message-text"
            v-html="message.from === 'ai' ? message.text : message.text"
          ></div>
        </div>
      </div>
      <!-- 加载动画 -->
      <div v-if="isLoading" class="ai-message">
        <div class="avatar">🤖</div>
        <div class="message-content">
          <div class="message-text loading">
            <span class="dot"></span>
            <span class="dot"></span>
            <span class="dot"></span>
          </div>
        </div>
      </div>
    </div>
    <div class="chat-footer">
      <input
        v-model="userInput"
        @keyup.enter="sendMessage"
        placeholder="Type your message..."
        :disabled="isLoading"
      />
      <button @click="sendMessage" :disabled="isLoading">Send</button>
    </div>
  </div>
</template>
<style scoped>
.chat-window {
  width: 100%;
  max-width: 600px;
  height: 600px;
  border: 1px solid #e0e0e0;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  margin: 0 auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.chat-header {
  background-color: #ffffff;
  color: #333;
  padding: 15px;
  text-align: center;
  font-weight: 600;
  border-bottom: 1px solid #e0e0e0;
}

.chat-body {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f8f9fa;
}

.chat-body > div {
  display: flex;
  align-items: flex-start;
  margin-bottom: 20px;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.message-content {
  margin-left: 12px;
  margin-right: 12px;
  max-width: 70%;
}

.message-text {
  padding: 12px 16px;
  border-radius: 18px;
  font-size: 14px;
  line-height: 1.4;
}

.user-message {
  flex-direction: row-reverse;
}

.user-message .message-text {
  background-color: #007aff;
  color: white;
  border-bottom-right-radius: 4px;
}

.ai-message .message-text {
  background-color: #e9ecef;
  color: #333;
  border-bottom-left-radius: 4px;
}

.chat-footer {
  display: flex;
  padding: 15px;
  border-top: 1px solid #e0e0e0;
  background-color: #fff;
}

.chat-footer input {
  flex: 1;
  padding: 12px;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  margin-right: 10px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.chat-footer input:focus {
  outline: none;
  border-color: #007aff;
}

.chat-footer button {
  padding: 8px 20px;
  background-color: #007aff;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.chat-footer button:hover:not(:disabled) {
  background-color: #0056b3;
}

.chat-footer button:disabled,
.chat-footer input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 加载动画样式 */
.loading {
  min-width: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin: 0 4px;
  border-radius: 50%;
  background-color: #666;
  animation: bounce 1.4s infinite ease-in-out;
}

.dot:nth-child(1) {
  animation-delay: -0.32s;
}

.dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: scale(0);
  }
  40% {
    transform: scale(1);
  }
}

@media (max-width: 600px) {
  .chat-window {
    width: 100%;
    height: 100vh;
    border-radius: 0;
  }

  .message-content {
    max-width: 85%;
  }
}
</style>
